package com.cac.demo.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AlgorithmUtil {

    /****
     * 平均分配，目前没有考虑分拣台已有的任务
     *
     * **/

    public static List aveDis(List<HashMap> tasks,int group){
        if(CollectionUtils.isEmpty(tasks)){
            return Lists.newArrayList();
        }
        List<List<HashMap>> result = Lists.newArrayList();
        Map<Integer, Set<HashMap>> temp = Maps.newHashMap();
        for(int i=0;i<tasks.size();i++){
            if(temp.containsKey(i%group)){
                Set<HashMap> ts = temp.get(i%group);
                ts.add(tasks.get(i));
            }else{
                Set<HashMap> ts = Sets.newHashSet();
                ts.add(tasks.get(i));
                temp.put(i%group,ts);
            }
        }
        for(Set<HashMap> ts:temp.values()){
            result.add(Lists.newArrayList(ts));
        }
        return result;
    }
}
